package com.yy.sso.user.controller;

import com.yy.sso.common.vo.Result;
import com.yy.sso.user.rest.IOrderRest;
import com.yy.sso.user.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private IOrderRest orderRest;

    @GetMapping(value = "/test")
    @PreAuthorize("hasAnyAuthority('user:add') and hasAnyRole('order')")
    public Result<List<String>> test(Principal principal) {
        List<String> objects = new ArrayList<>();
        objects.add("用户服务测试成功");
        objects.add(principal.getName());
        return Result.ok(objects);
    }

    @GetMapping(value = "/test1")
    public Result<Authentication> test( ) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Result.ok(authentication);
    }

    @PostMapping(value = "/{userId}/order")
     public Result<List<OrderVo>> getOrderListByUserId(@PathVariable("userId")Long userId){
        return this.orderRest.getOrderListByUserId(userId);
     }
}
